function ajaxGlobalSetting(){
	//ajax global setting, append ajax flag parameter
	$.ajaxSetup({
		data:{
			ajax:"ajax"
		}
	});
}
function isNumber(value){
	return parseFloat(value)==value;
}
function refreshYear() {
	$("select[name=schoolYearNo]").html("<option>...</option>");
	$.ajax({
		dataType : "json",
		url : "../../year.do?method=get",
		cache: false,
		success : function(msg) {
			if (!msg.errorMsg) {
				var content = "<option selected=\"selected\">请选择...</option>";
				$.each(msg, function(i) {
					content += "<option value='" + msg[i].schoolYearNo + "'>"
							+ msg[i].schoolYearName + "</option>";
				});
				$("select[name=schoolYearNo]").html(content);
			}
		}
	});
}
function initUnits(select,levelNo,parentUnitNo){
	if(select){
		select.html("<option>...</option>");
		var url = "../unit.do?method=get";
		var params = {
				unitLevelNo:levelNo,
				parentUnitNo:parentUnitNo
		};
		$.ajax({   
			dataType: "json",
			cache: false,
			url: url,
			data: params,
			success: function(result){
				if(result&&!result.errorMsg){
					var content = "<option selected=\"selected\">请选择...</option>";
					$.each(result,function(i){
						content += "<option value='" + result[i].unitNo + "' levelNo='"+result[i].unitLevel.levelNo+"'>" + result[i].unitName + "</option>";
					});
					select.html(content);
				}
			}
		});
	}
}
function getItems(){
	$.ajax({
		dataType: "json",
		url: "../item.do?method=get",
		cache: false,
		success: function(result){
			if(result&&!result.errorMsg){
				var content = "<option selected='selected'>请选择...</option>";
				$.each(result,function(i,r){
					content+="<option value='"+r.itemNo+"' cost='"+r.cost+"'>"+r.itemName+"</option>";
				});
				$("#tabs-1").data("options",content);
			}
	}});
}
function unitSelectOnChanged(){
	var unitNo = this.options[this.selectedIndex].value;
	var nextSelect = $(this).parent().next().find("select");
	if($(this).parents("#tabs-2").length>0){
		if(this==$(".unitGroup:last").get(0)&&isNumber(unitNo)){
			$("#searchStuBtn").attr("disabled",false);
			$(".upload").show();
		}else{
			$("#searchStuBtn").attr("disabled",true);
			$(".upload").hide();
		}
	}
	if(!isNumber(unitNo)&&nextSelect&&nextSelect.length>0){
		$(this).parent().nextAll().find("select").html("<option selected=\"selected\">请选择...</option>");
		return;
	}
	if(nextSelect&&nextSelect.length>0)
		initUnits($(nextSelect), null, unitNo);
}
function changePwdDialog(id){
	$( "#changePwdDialog" ).dialog({
		autoOpen: false,
		height: 300,
		width: 350,
		modal: true,
		buttons: {
			"提交": function() {
				var origPwd = $("#origPwd",$("#changePwdDialog"));
				var pwd = $("#pwd",$("#changePwdDialog")).val();
				var confirm = $("#confirm",$("#changePwdDialog")).val();
				if(!checkLength(pwd,6,8)||!checkLength(confirm,6,8)){//stu
					alert("密码在6-8位之间！");
					return;
				}
				if(!checkRegexp(pwd,/^([0-9a-zA-Z])+$/)){
					alert("允许字符为a-z和0-9");
					return;
				}
				if(pwd!=confirm){
					alert("密码输入不一致，请重新输入！");
					return;
				}
				var form = $("#changePwdDialog>form");
				$("input[name='stuNo']",form).val(id);
				var action = form.attr("action");
				$.ajax({   
					type:"post",
					url: action,
					data: form.serialize(),
					success: function(result){
						form.get(0).reset();
						if(result&&!result.errorMsg||!result){
							alert("修改成功");
							$( "#changePwdDialog" ).dialog( "close" );
						}else{
							alert(result.errorMsg);
						}
					}
				});
			},
			"取消": function() {
				$("#changePwdDialog>form").get(0).reset();
				$( this ).dialog( "close" );
			}
		}
	});
	$( "#changePwdDialog" ).dialog( "open" );
}
function checkRegexp(value,regexp) {
	if ( !( regexp.test( value ) ) ) {
		return false;
	} else {
		return true;
	}
}
function checkLength( v, min, max ) {
	if ( v.length > max || v.length < min ) {
		return false;
	} else {
		return true;
	}
}
function downLoadUploadTemplate(){
	$("#uploadTemplate").bind("click",function(){
		window.open("../download.do?method=template");
	});
}
function downloadReport(){
	$("#downloadReport").bind("click",function(){
		var yearSelect = $(this).parent().parent().find("select[name='schoolYearNo']");
		if(!isNumber(yearSelect.val())){
			alert("请选择学年");
			return;
		}
		var yearNo = yearSelect.val();
		window.open("../download.do?method=report&schoolYearNo="+yearNo);
	});
}
//add by chamdenjo@gmail.com
function downloadUnit(){
	$("#downloadUnit").bind("click",function(){
		window.open("../download.do?method=downloadAllUnit");
	});
}
function downloadUnitTemplate(){
	$("#downloadUnitTemplate").bind("click",function(){
		window.open("../download.do?method=unitTemplate");
	});
}
//end add
function downloadStuReport(){
	$("#downloadStuReport").bind("click",function(){
		var option = $("#tabs-2").find(".unitGroup:last option[levelNo]:selected");
		var unitNo=option.val();
		if(!isNumber(unitNo)){
			alert("请选择班级！");
			return false;
		}
		window.open("../download.do?method=downloadStudents&unitNo="+unitNo);
	});
}
function uploadFormOnSubmit(form){
	var option = $(form).parent().parent().find(".unitGroup:last option[levelNo]:selected");
	var unitNo=option.val();
	if(!isNumber(unitNo)){
		alert("请选择班级！");
		return false;
	}
	form.unitNo.value = unitNo;
	return true;
}

//add by chamdenjo@gmail.com
function uploadUnitOnSubmit(form){
	
	return true;
}
//end add
function initRecordOrder(){
	$("#recordDescrption").dialog({
		autoOpen: false,
		resizable: false,
		height: 160,
		modal: true
	});
}
function initPaymentDialog(){
	$("#stuPaymentDetail").dialog({
		autoOpen: false,
		width: 460,
		modal: true,
		buttons: {
			"生成转账单": function() {
				toOrder();
			},
			"取消": function() {
				$( this ).dialog( "close" );
			}
		}
	});
}
function toOrder(){
	var tabSelected = $( "#tabs" ).tabs( "option", "selected" );
	var yearNoOption = $("#tabs-"+(tabSelected+1)).find("select[name=schoolYearNo] option:selected");
	var yearNo = yearNoOption.val();
	var yearName = yearNoOption.html();
	var data = $("#stuPaymentDetailList").getRowData();
	var selected = $("#stuPaymentDetailList").getGridParam('selarrrow');
	if(selected.length==0){
		alert("请勾选需缴费项");
		return;
	}
	var stuNo = $( "#stuPaymentDetail" ).data("stuNo");
	var params = {schoolYearNo:[],stuNo:[],itemPaymentNo:[],payment:[]};
	var total = 0;
	var isExistsPaidNotSelect = false;
	$.each(data,function(i,d){
		if($.inArray(d.itemPaymentNo+"",selected)!=-1){
			params.schoolYearNo[params.schoolYearNo.length] = yearNo;
			params.stuNo[params.stuNo.length] = stuNo;
			params.itemPaymentNo[params.itemPaymentNo.length] = d.itemPaymentNo;
			params.payment[params.payment.length] = d.cost;
			total+=parseFloat(d.cost);
		}else{
			if(parseInt(d.itemPaymentNo)==-1){
				isExistsPaidNotSelect = true;
				return false;
			}
		}
	});
	if(isExistsPaidNotSelect){
		alert("请勾选已支付项！");
		return;
	}
	if(total<=0){
		alert("转账金额必须大于零!");
		return;
	}
	if(!confirm("生成"+yearName+"学费转账单？")){return;}
//	alert(decodeURIComponent($.param(params)));
//	return;
	$.post("../../pay.do?method=add", params, function(result) {
		if(result&&!result.errorMsg){
			var confirm4Record = {
				text: "确认转账",
				click: function() {
					/*
					$.post("../../pay.do?",{method:"finish",paymentRecordNo:result.recordNo,money:result.total},function(msg){
						if(msg&&!msg.errorMsg){
							alert("转账成功");
							$("#recordDescrption").dialog("close");
						}else{
							alert(msg.errorMsg);
						}
					},'json');
					*/
					$("#recordDescrption").dialog("close");
					$("#stuPaymentDetail").dialog("close");
					window.open("../../pay.do?method=finish&paymentRecordNo="+result.recordNo+"&money="+result.total,"null","");
				}
			};
			$("#recordInfo").html("生成转账单号："+result.recordNo+"<br/>转账金额：&yen"+result.total);
			$("#recordDescrption").dialog( "option", "buttons", [confirm4Record,cancel4Record] );
		}else{
			$("#recordInfo").text(result?result.errorMsg:"生成转账单错误！");
			$("#recordDescrption").dialog( "option", "buttons", [cancel4Record] );
		}
		$("#recordDescrption").dialog("open");
	}, "json");
}
var cancel4Record = {
	text: "取消",
    click: function() { $(this).dialog("close"); }
};
/**GRID CONFIGURATION*/
function gridOperationAfterSubmit(data){
	var result = $.parseJSON(data.responseText);
	if(result&&result.errorMsg){
		return [false,result.errorMsg];
	}
	return [true];
}

/*YEAR LIST*/
function initYearList(){
	$("#yearList").jqGrid({
		url:'../year.do',
		datatype: "json",
		width: 890,
	   	colNames:['学年#','学年名'],
	   	viewrecords: true,
	   	sortname: 'schoolYearNo',
	    sortorder: "desc",
	   	rowNum:10,
	   	rowList:[10,20,30],
	   	pager: $('#yearListPager'), 
	   	colModel:[
				{name:'schoolYearNo',index:'schoolYearNo',align: 'center',sorttype: 'integer',editable:true,editoptions:{readonly:true}},
	   	   		{name:'schoolYearName',index:'schoolYearName',align: 'center', sortable:false,editable:true,editrules:{required:true}}
	   	],
	   	jsonReader: {
			repeatitems : false,
			id: "0"
		}
	}).navGrid('#yearListPager',{search:false},yearGridEdit,yearGridAdd,yearGridDel,{});
}
var yearGridEdit = {
	height : 150,
	url : '../year.do?method=edit',
	afterSubmit: afterSubmit4Year
};
var yearGridAdd = {
	height : 150,
	url : '../year.do?method=add',
	afterSubmit: afterSubmit4Year
};
var yearGridDel = {
	url : '../year.do?method=del',
	serializeDelData : function(data) {
		return $("#yearList").getRowData(data.id);
	},
	afterSubmit: afterSubmit4Year
};
function afterSubmit4Year(data){
	var result = gridOperationAfterSubmit(data);
	if(result[0]==true){
		refreshYear();
	}
	return result;
}
/*ITEM LIST*/
function initItemList(){
	$("#itemList").jqGrid({
		url:'../item.do',
		datatype: "json",
		prmNames: {id:"itemNo",oper:"method"},
		width: 890,
	   	colNames:['款项名','金额'],
	   	viewrecords: true,
	   	sortname: 'itemNo',
	    sortorder: "desc",
	   	rowNum:10,
	   	rowList:[10,20,30],
	   	pager: $('#itemListPager'), 
	   	colModel:[
				{name:'itemName',index:'itemName',align: 'center',editable:true,sortable:false,editrules:{required:true}},
	   	   		{name:'cost',index:'cost',align: 'center', sortable:true,editable:true,editrules:{required:true,number:true},formatter:'currency', formatoptions:{thousandsSeparator:""}}
	   	],
	    editurl:"../item.do",
	   	jsonReader: {
			repeatitems : false,
			id: "itemNo"
		}
	}).navGrid('#itemListPager',{search:false},itemGridEdit,itemGridAdd,itemGridDel,{});
}
var itemGridEdit = {
	height : 150,
	afterSubmit: afterSubmit4Item
};
var itemGridAdd = {
	height : 150,
	afterSubmit: afterSubmit4Item
};
var itemGridDel = {
	afterSubmit: afterSubmit4Item
};
function afterSubmit4Item(data){
	var result = gridOperationAfterSubmit(data);
	if(result[0]==true){
		getItems();
	}
	return result;
}
/*UNIT LIST*/
function searchUnitBtnOnClick(){
	var yearSelect = $(this).parent().parent().find("select[name='schoolYearNo']");
	if(!isNumber(yearSelect.val())){
		alert("请选择学年");
		return;
	}
	var option = $(this).parent().parent().find(".unitGroup option[levelNo]:selected:last");
	var levelNo,parentUnitNo;
	if(option.length==0){
		parentUnitNo = 0;
		levelNo = 1;
	}else{
		parentUnitNo=option.val();
		levelNo=parseInt(option.attr("levelNo"))+1;
	}
	$("#unitList").jqGrid('setGridParam', {
		url : "../unit.do?parentUnitNo=" + parentUnitNo+"&unitLevelNo="+levelNo,
		page : 1,
		datatype: "json",
		editurl:"../unit.do?parentUnitNo="+parentUnitNo+"&unitLevelNo="+levelNo
	}).trigger("reloadGrid");
}
function initUnitList(){
	$("#unitList").jqGrid({
		datatype: "local",
		prmNames: {id:"unitNo",oper:"method"},
		width: 890,
	   	colNames:['单位名','隶属','层级'],
	   	viewrecords: true,
	   	rowNum:10,
	   	sortname: 'unitNo',
	    sortorder: "desc",
	   	rowList:[10,20,30],
	   	pager: $('#unitListPager'), 
	   	colModel:[
				{name : 'unitName',index : 'unitName',align : 'center',editable : true,sortable : false,editrules : {required : true},formatter:'showlink', formatoptions:{baseLinkUrl:'#'}},
				{name:'parentUnit.unitName',index:'parentUnit.unitName',align: 'center',editable:true,sortable:false,editoptions:{readonly:true}},
	   	   		{name:'unitLevel.levelName',index:'unitLevel.levelName',align: 'center', sortable:false,editable:true,editoptions:{readonly:true}}
	   	],
	   	jsonReader: {
			repeatitems : false,
			id: "unitNo",
			parentUnitNo: "parentUnit.unitNo",
			unitLevelNo: "unitLevel.levelNo"
		},
		loadComplete: initUnitListLink
	}).navGrid('#unitListPager',{search:false,refresh:false},unitGridNav,unitGridNav,{
		afterSubmit : afterSubmit4Unit
	},{});
}
var unitGridNav = {
	height : 200,
	afterSubmit : afterSubmit4Unit
};
function afterSubmit4Unit(data,postdata){
	var result = gridOperationAfterSubmit(data);
	if(result[0]==true){
		var select = $("#tabs-1 .unitGroup option[levelNo]:selected:last").parent().next("select");
		if(select.length==1)
			initUnits(select,select.attr("name").split("_")[1]);
	}
	return result;
}
/*
 * get the payment status for student
 */
function initPaymentList(){
	$("#paymentList").jqGrid({
		datatype: "local",
		prmNames: {id:"stuNo",oper:"method"},
		width: 890,
	   	colNames:['学号','学年','支付金额','支付完成时间'],
	   	viewrecords: true,
	   	rowNum:10,
	   	sortname: 'stuNo',
	    sortorder: "desc",
	   	rowList:[10,20,30],
	   	pager: $('#paymentListPager'), 
	   	colModel:[
	   	        {name:'student.stuNo',index : 'student.stuNo',align : 'center',editable:true,sortable : false,editrules : {required : true,number:true},formatter:'showlink', formatoptions:{baseLinkUrl:'#'}},
				{name:"schoolYear.schoolYearName",index:"schoolYearName",align : 'center',editable : true,sortable : false,editrules : {required : true},formatter:'showlink', formatoptions:{baseLinkUrl:'#'}},
				{name:"total",index:"total",width:100,align:"right",sortable:false,formatter:'currency', formatoptions:{thousandsSeparator:""}},
                {name:"datetime",index:"datetime"}
	   	],
	   	jsonReader: {
			repeatitems : false,
			id: "student.stuNo"
		},
		loadComplete: initUnitListLink
	}).navGrid('#paymentListPager',{search:false,refresh:false},unitGridNav,unitGridNav,{
		afterSubmit : afterSubmit4Unit
	},{});
}
function initUnitListLink(grid){
	var rows = grid.rows;
	$("#unitList a").bind("click",function(){
		var unitNo = $(this).parent().parent().attr("id");
		var yearNo = $("select[name=schoolYearNo]:first option:selected").val();
		$("#unitPaymentDetail").dialog("open");
		$("#unitPaymentDetailList").jqGrid('setGridParam', {
			url : "../item4unit.do?unitNo="+unitNo+"&schoolYearNo="+yearNo,
			page : 1,
			datatype: "json",
			editurl:"../item4unit.do?unitNo="+unitNo+"&schoolYearNo="+yearNo
		}).trigger("reloadGrid");
	});
}
/*UNIT ITEM LIST*/
function initUnitPaymentDetailList(){
	$("#unitPaymentDetailList").jqGrid({
		datatype: "local",
		prmNames: {id:"itemPaymentNo",oper:"method"},
		width: 430,
	   	colNames:['缴款项','金额','备注','款项#'],
	   	viewrecords: true,
	   	rowNum:30,
	   	rowList:[10,20,30],
	   	pager: $('#unitPaymentDetailListPager'), 
	   	colModel:[
				{name:'item.itemName',index : 'itemName',align : 'center',editable : true,sortable : false,editrules : {required : true},edittype:'custom', editoptions:{custom_element: itemElem, custom_value:itemPostData}},
				{name:'cost',index:'cost',align: 'center',editable:true,sortable:false,editrules : {required : true,number:true},formatter:'currency', formatoptions:{thousandsSeparator:""}},
				{name:'comment',index:'comment',align: 'left',editable:true,sortable:false,edittype:'textarea'},
				{name:'item.itemNo',index:'itemNo',align: 'center',hidden:true,editable:true,sortable:false}
	   	],
		loadComplete: calcTotalCost,
		footerrow : true,
		userDataOnFooter : true,
	   	jsonReader: {
			repeatitems : false,
			id: "itemPaymentNo"
		}
	}).navGrid('#unitPaymentDetailListPager',{search:false,refresh:false},unitPaymentGridNav,unitPaymentGridNav,unitPaymentGridNav,{});
}
var unitPaymentGridNav={
	zIndex: 1500,
	height : 200,
	recreateForm:true,
	onclickSubmit:function(params, posdata){
		return {itemNo:posdata["item.itemNo"]};
	},
	afterSubmit : gridOperationAfterSubmit
};
function itemElem(value, options){
	var options = $("#tabs-1").data("options");
	var sel = document.createElement("select");
	$(sel).html(options);
	$(sel).children("option").each(function(){
		if(this.innerHTML==value){
			$(this).attr("selected",true);
			return false;
		}
	});
	$(sel).bind("change",function(){
		var option = this.options[this.selectedIndex];
		var itemNo = option.value;
		var cost = $(option).attr("cost");
		if(isNumber(cost)){
			$("#cost").val(cost);
		}else{
			$("#cost").val("");
		}
		if(isNumber(itemNo)){
			document.getElementById("item.itemNo").value=itemNo;
		}
	});
	return sel;
}
function itemPostData(elem, operation, value){
	return document.getElementById("item.itemNo").value;
}
/*STUDENT LIST*/
function searchStuBtnOnClick(){
	var yearSelect = $(this).parent().parent().find("select[name='schoolYearNo']");
	if(!isNumber(yearSelect.val())){
		alert("请选择学年");
		return;
	}
	var option = $(this).parent().parent().find(".unitGroup:last option[levelNo]:selected");
	var unitNo=option.val();
	$("#stuList").jqGrid('setGridParam', {
		url : "../stu.do?unitNo="+unitNo,
		page : 1,
		datatype: "json",
		editurl:"../stu.do?unitNo="+unitNo
	}).trigger("reloadGrid");
}
function initStuList(edit){
	$("#stuList").jqGrid({
		datatype: "local",
		prmNames: {id:"stuNo",oper:"method"},
		width: 890,
		height: "auto", 
		hiddengrid: true,
	   	colNames:['学号','姓名','所属班级',''],
	   	viewrecords: true,
	   	rowNum:10,
	   	rowList:[10,20,30],
	   	sortname: 'stuNo',
	    sortorder: "desc",
	   	pager: $('#stuListPager'), 
	   	colModel:[
				{name:'stuNo',index : 'stuNo',align : 'center',editable:true,sortable : false,editrules : {required : true,number:true}},
				{name:'stuName',index:'stuName',align: 'center',editable:true,sortable:false,editrules : {required : true}},
	   	   		{name:'unit.unitName',index:'unit.unitName',align: 'center', sortable:false,editable:true,editoptions:{readonly:true}},
	   	   		{name:'action',index:'action',align: 'center', sortable:false,editable:false}
	   	],
	   	jsonReader: {
			repeatitems : false,
			id: "stuNo"
		},
		gridComplete : function() {
			var ids = $("#stuList").jqGrid('getDataIDs');
			for ( var i = 0; i < ids.length; i++) {
				var cl = ids[i];
				be = "<input stuNo='"+cl+"' class='changePwdBtn' type='button' value='改密'/>";
				be+="&nbsp;<input stuNo='"+cl+"' class='paymentListBtn' type='button' value='缴费清单'/>";
				$("#stuList").jqGrid('setRowData',cl,{action:be});
			}
			$(".changePwdBtn").button().click(function(){changePwdDialog($(this).attr("stuNo"));});
			$(".paymentListBtn").button().click(function(){showPaymentListDialog($(this).attr("stuNo"));});
		},	
		subGrid : true,
	    subGridRowExpanded: function(subgrid_id, row_id) {
	           var subgrid_table_id;
	           subgrid_table_id = subgrid_id+"_t";
	           $("#"+subgrid_id).html("<table id='"+subgrid_table_id+"' class='scroll'></table>");
	           $("#"+subgrid_table_id).jqGrid({
	              url:"../../fin.do?stuNo="+row_id,
	              datatype: "json",
	              colNames: ['学年','支付金额','支付完成时间'],
	              colModel: [
	                {name:"schoolYear.schoolYearName",index:"schoolYearName",width:100,key:true,sortable:false},
	                {name:"total",index:"total",width:100,align:"right",sortable:false,formatter:'currency', formatoptions:{thousandsSeparator:""}},
	                {name:"datetime",index:"datetime",width:170}
	              ],
	              height: "100%", 
		      	   	jsonReader: {
		    			repeatitems : false,
		    			cell:""
		    		},
	              rowNum: 20
	           });
	    }
	}).navGrid('#stuListPager',{search:false,refresh:false,add:edit,edit:edit,del:edit},stuGridNav,stuGridNav,stuGridNav,{});
}
var stuGridNav = {
	height : 200,
	afterSubmit : gridOperationAfterSubmit
};
function showPaymentListDialog(stuNo){
	$( "#stuPaymentDetail" ).data("stuNo",stuNo);
	var tabSelected = $( "#tabs" ).tabs( "option", "selected" );
	var yearNo = $("#tabs-"+(tabSelected+1)).find("select[name=schoolYearNo] option:selected").val();
	var title = "学生缴费清单 > 学号："+stuNo+" 姓名："+$("#stuList").getRowData(stuNo).stuName;
	$( "#stuPaymentDetail" ).dialog( "option", "title", title );
	$("#stuPaymentDetail").dialog("open");
	$("#stuPaymentDetailList").jqGrid('setGridParam', {
		url : "../../item4stu.do?stuNo="+stuNo+"&schoolYearNo="+yearNo,
		page : 1,
		datatype: "json",
		editurl:"../item4stu.do?stuNo="+stuNo+"&schoolYearNo="+yearNo
	}).trigger("reloadGrid");
}
/*STUDENT ITEM LIST*/
function initStuPaymentDetailList(){
	$("#stuPaymentDetailList").jqGrid({
		datatype: "local",
		prmNames: {id:"itemPaymentNo",oper:"method"},
		width: 430,
	   	colNames:['单位','缴款项','金额','备注','款项#'],
	   	viewrecords: true,
	   	rowNum:30,
	   	rowList:[10,20,30],
	   	pager: $('#stuPaymentDetailListPager'), 
	   	colModel:[
				{name:'unit.unitName',index:'unit.unitName',align: 'center',editable:true,sortable:false,editoptions:{readonly:true}},
				{name:'item.itemName',index : 'itemName',align : 'center',editable : true,sortable : false,editrules : {required : true},edittype:'custom', editoptions:{custom_element: itemElem, custom_value:itemPostData}},
				{name:'cost',index:'cost',align: 'center',editable:true,sortable:false,editrules : {required : true,number:true},formatter:'currency', formatoptions:{thousandsSeparator:""}},
				{name:'comment',index:'comment',align: 'left',editable:true,sortable:false,edittype:'textarea'},
				{name:'item.itemNo',index:'itemNo',align: 'center',hidden:true,editable:true,sortable:false}
	   	],
	   	jsonReader: {
			repeatitems : false,
			id: "itemPaymentNo"
		},
		loadComplete: calcTotalCost,
		footerrow : true,
		userDataOnFooter : true
	}).navGrid('#stuPaymentDetailListPager',{search:false,refresh:false},stuPaymentGridNav,stuPaymentGridNav,stuPaymentGridNav,{});
}
var stuPaymentGridNav={
		zIndex: 1500,
		height : 200,
		recreateForm:true,
		beforeSubmit: stopUnitEdit,
		onclickSubmit:function(params, posdata){
			return {itemNo:posdata["item.itemNo"]};
		},
		afterSubmit : gridOperationAfterSubmit
};
function stopUnitEdit(t){
	var id = t;
	if(t.stuPaymentDetailList_id){
		id = t.stuPaymentDetailList_id;
	}
	var result = [true,""];
	if(id==-1){
		result = [false,"不能修改已支付项"];
		return result;
	}
	var row = $("#stuPaymentDetailList").getRowData(id);
	$.each(row,function(i,value){
		if(i=='unit.unitName'&&value){
			result=[false,"创建学生支付项以覆盖学校支付设置"];
			return false;
		}
	});
	return result;
}
function calcTotalCost(data){
	var rows = data.rows;
	var total = 0;
	var multiSelect = this.p.multiselect;
	var multiSelArr = this.p.selarrrow;
	$.each(rows,function(i,r){
		if(multiSelect&&$.inArray(r.itemPaymentNo+"",multiSelArr)==-1){
			return true;
		}
		total+=parseFloat(r.cost);
	});
	var grid = this;
	$("#"+grid.id).footerData("set",{"item.itemName":"合计","cost":total},true);
}
function reCalcTotalCost(){
	var data = {"rows":this.p.data};
	calcTotalCost.call(this,data);
}
function initStuPaymentDetail4PaymentList(){
	var lastSel;
	$("#stuPaymentDetailList").jqGrid({
		datatype: "local",
		prmNames: {id:"itemPaymentNo",oper:"method"},
		width: 430,
	   	colNames:['#','缴款项','金额','备注','款项#'],
	   	viewrecords: true,
	   	rowNum:30,
	   	rowList:[10,20,30],
	   	pager: $('#stuPaymentDetailListPager'), 
	   	colModel:[
				{name:'itemPaymentNo',index:'itemPaymentNo',align: 'center',hidden:true,editable:true,sortable:false},
				{name:'item.itemName',index : 'itemName',align : 'center',editable : false,sortable : false,editrules : {required : true}},
				{name:'cost',index:'cost',align: 'center',editable:true,sortable:false,editrules : {required : true,number:true},formatter:'currency', formatoptions:{thousandsSeparator:""}},
				{name:'comment',index:'comment',align: 'left',editable:false,sortable:false,edittype:'textarea'},
				{name:'item.itemNo',index:'itemNo',align: 'center',hidden:true,editable:true,sortable:false}
	   	],
	   	jsonReader: {
			repeatitems : false,
			id: "itemPaymentNo"
		},
		loadComplete: calcTotalCost,
		footerrow : true,
		userDataOnFooter : true,
		loadonce: true,
		multiselect: true,
		onSelectRow: reCalcTotalCost,
		onSelectAll: reCalcTotalCost,
		ondblClickRow : function(id) {
			if (id&&parseInt(id)!=-1) {
				$('#stuPaymentDetailList').editRow(id,true,function(rowid){
					var row = $("#stuPaymentDetailList").jqGrid("getInd",rowid,true);
					$("input[name='cost']",row).focus();
					$("input[name='cost']",row).bind("blur",function(){
						$('#stuPaymentDetailList').saveRow(rowid, null, 'clientArray', {}, reCalcTotalCost);
					});
				},null,'clientArray',{},reCalcTotalCost);
				lastSel = id;
			}
		}
	});
}
/*TEACHER LIST*/
function initTeacherList(){
	$("#teacherList").jqGrid({
		url:'../admin.do',
		datatype: "json",
		prmNames: {id:"teacherNo",oper:"method"},
		width: 450,
	   	colNames:['登录名','密码'],
	   	viewrecords: true,
	   	sortname: 'teacherNo',
	    sortorder: "desc",
	   	rowNum:10,
	   	rowList:[10,20,30],
	   	pager: $('#teacherListPager'), 
	   	colModel:[
				{name:'loginid',index:'loginid',align: 'center',editable:true,sortable:false,editrules:{custom:true, custom_func:limitLength}},
	   	   		{name:'pwd',index:'pwd',align: 'center', hidden:true,editable:true,edittype:'password',editoptions: {value:""},editrules:{edithidden:true,custom:true, custom_func:limitLength}}
	   	],
	    editurl:"../admin.do",
	   	jsonReader: {
			repeatitems : false,
			id: "teacherNo"
		}
	}).navGrid('#teacherListPager',{search:false},gridOperationAfterSubmit,gridOperationAfterSubmit,gridOperationAfterSubmit,{});
}
function limitLength(value, colname){
	var v = $.trim(value);
	if(!checkLength(v,6,8))
		return [false,colname+"长度介于6-8位之间"];
	else{
		if(!checkRegexp(value,/^([0-9a-zA-Z])+$/)){
			return [false,colname+"允许字符为a-z和0-9"];
		}
		return [true,""];
	}
}
/**GRID CONFIGURATION END*/